local screen = {guiGetScreenSize()};
scale = 1;
if screen[1] < 1920 then scale = math.min(2, 1920/screen[1]); end

local markers = {};
local table = {};

markers.rt = {};
markers.font = dxCreateFont(':cmta-pobieraniezasobow/OSB.ttf', 18 / scale, false, 'cleartype_natural');
markers.disc = dxCreateTexture('disc.png', 'argb', true, 'clamp');

markers.icon = {};
markers.icons = {
    ['job'] = 'job',
    ['shirt'] = 'shirt',
    ['cars'] = 'cars',
    ['mining'] = 'mining',
    ['tuning'] = 'tuning',
    ['stations'] = 'stations',
    ['garage'] = 'garage',
    ['box'] = 'box',
    ['insidebox'] = 'insidebox',
    ['diagnostic'] = 'diagnostic',
    ['weave'] = 'weave',
    ['repair'] = 'repair',
    ['race'] = 'race',
    ['sell'] = 'sell',
    ['buy'] = 'buy',
    ['work'] = 'work',
    ['license'] = 'license',
    ['increase'] = 'increase',
    ['house'] = 'house',
    ['dealer'] = 'dealer',
    ['mechanic'] = 'mechanic',
    ['towtruck'] = 'towtruck',
    ['leader'] = 'leader',
    ['fractionparking'] = 'fractionparking',
    ['strzalka'] = 'strzalka',
    ['destroying-car'] = 'destroying-car',
};

tick = getTickCount()
local sinus = math.sin
markers.preRender = function()
        local sin = sinus(getTickCount() / 1200) / 14;
        for i, v in pairs(getElementsByType('marker')) do
            if markers.rt[v] then
                if getMarkerType(v) == 'cylinder' then
                    local dist = getDistanceBetweenPoints3D(Vector3(getElementPosition(localPlayer)), Vector3(getElementPosition(v)));
                    if dist < 30 then 
                        local data = getElementData(v, 'marker:data');
                        if data then
                            local size = getMarkerSize(v);
                            local x, y, z = getElementPosition(v);
                            local r2,g2,b2=interpolateBetween(80,80,80,255,0,0,(getTickCount()-tick)/3000,"CosineCurve");
                            if string.len(data.name) > 0 then
                            local markerline = dxDrawMaterialLine3D(x+size/3, y+size/3, z - 0.95, x-size/3, y-size/3, z-0.95, markers.disc, size-0.1, tocolor(r2, g2, b2, 200),x,y,z);
                            end
                            if markers.icon[v] then
                                dxDrawMaterialLine3D(x, y, z + data.size + sin, x, y, z + sin, markers.icon[v], data.size);
                            end
                            dxDrawMaterialLine3D(x, y, z - 0.17, x, y, z - 0.3, markers.rt[v], 1);
                        end
                    end
                end
            end
        end
    end
addEventHandler('onClientPreRender', root, markers.preRender);

for i, v in pairs(getElementsByType('marker')) do
    local data = getElementData(v, 'marker:data');
    if data then
        setMarkerColor(v, 0, 0, 0, 0);
    end
end 

addEventHandler('onClientElementStreamIn', root, function()
    if getElementType(source) == 'marker' then
        local data = getElementData(source, 'marker:data');
        if data then

            markers.icon[source] = dxCreateTexture('icons/'..markers.icons[data.icon]..'.png', 'argb', true, 'clamp')
            markers.rt[source] = dxCreateRenderTarget(200 / scale, 50 / scale, true);
    
            dxSetRenderTarget(markers.rt[source], true);
            if string.len(data.name) > 0 then
            roundedRectangle(0, 0, 200 / scale, 50 / scale, 0xffff0000, 0xffd2691e);
            dxDrawText(data.name or '-', 0, 0, 200 / scale, 53 / scale, 0xFFD4D4D4, 1, markers.font, 'center', 'center');
            end
            dxSetBlendMode('modulate_add');
            dxSetRenderTarget();
        end
    end
end);

addEventHandler('onClientElementStreamOut', root, function()
    if getElementType(source) == 'marker' then
        if markers.rt[source] then
            markers.rt[source] = nil;
        end
        if markers.icon[source] then
            destroyElement(markers.icon[source])
        end
    end 
end);
